import {Article as articleModol, Types as typesModol} from '../../modol/Article'
import unifyResponse from '../../methods/responseConfig'


class Archives{
    
    constructor(){}
    
    async classifyByType(req, res, next){
    	try
    	{
    		let page;
            if(!req.query.type)
            throw new error('类型不能为空');
            if(req.query.page==undefined)
            page = 0;
            else
            page = Number(req.query.page) - 1;
            let data = await articleModol.find({Article_type:req.query.type},{"_id":0,"__v":0,"Article_sort":0,"Article_content":0})
              .sort({"Article_sort":-1,"_id":-1}).skip(page*5).limit(5);
            let allNumber = await articleModol.find({Article_type:req.query.type}).count();
            let leftPage = (page==0)?-1:page,rightPage;
            let types = await typesModol.find({},{"_id":0,"__v":0});
            console.log(leftPage);
            if(allNumber>page*5+5)
            rightPage = page+2;
            else
            rightPage = -1;
            res.render('type',{articles: data, page: page+1, leftPage: leftPage, rightPage: rightPage, type: req.query.type, types: types});
    	}
    	catch(err)
    	{
            console.log(err.message, err);
            unifyResponse(res,0,'GET_TYPR_ERROR',err.message);
		    return 
    	}
    }
}

export default new Archives()